﻿using IS_Ever_Green.DataAccess.Model;
using Microsoft.EntityFrameworkCore;

namespace IS_Ever_Green.DataAccess
{
    public class EverGreenDBContext : DbContext
    {

        public EverGreenDBContext()
        {

        }

        public DbSet<CartModel> Carts { get; set; }
        public DbSet<CategoryModel> Categories { get; set; }
        public DbSet<ImportStatus> ImportStatus { get; set; }
        public DbSet<ManufacturerModel> Manufacturers { get; set; }
        public DbSet<OrderItemModel> OrderItems { get; set; }
        public DbSet<OrderModel> Orders { get; set; }
        public DbSet<ProductModel> Products { get; set; }
        public DbSet<ReviewModel> Reviews { get; set; }
        public DbSet<RoleModel> Roles { get; set; }
        public DbSet<UserModel> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=CODESAGE;Database=EverGreen;Trusted_Connection=True;Encrypt=False");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Корзина (CartModel) связана с пользователем (UserModel) и продуктом (ProductModel)
            modelBuilder.Entity<CartModel>()
                .HasOne(cart => cart.User)
                .WithMany(user => user.Carts)
                .HasForeignKey(cart => cart.UserId);

            modelBuilder.Entity<CartModel>()
                .HasOne(cart => cart.Product)
                .WithMany(product => product.Carts)
                .HasForeignKey(cart => cart.ProductId);

            // Категория (CategoryModel) связана с продуктом (ProductModel)
            modelBuilder.Entity<ProductModel>()
                .HasOne(product => product.Category)
                .WithMany(category => category.Products)
                .HasForeignKey(product => product.CategoryId);

            // Производитель (ManufacturerModel) связан с продуктом (ProductModel)
            modelBuilder.Entity<ProductModel>()
                .HasOne(product => product.Manufacturer)
                .WithMany(manufacturer => manufacturer.Products)
                .HasForeignKey(product => product.ManufacturerId);

            // Элемент заказа (OrderItemModel) связан с заказом (OrderModel) и продуктом (ProductModel)
            modelBuilder.Entity<OrderItemModel>()
                .HasOne(orderItem => orderItem.Order)
                .WithMany(order => order.OrderItems)
                .HasForeignKey(orderItem => orderItem.OrderId);

            modelBuilder.Entity<OrderItemModel>()
                .HasOne(orderItem => orderItem.Product)
                .WithMany(product => product.OrderItems)
                .HasForeignKey(orderItem => orderItem.ProductId);

            // Заказ (OrderModel) связан с пользователем (UserModel)
            modelBuilder.Entity<OrderModel>()
                .HasOne(order => order.User)
                .WithMany(user => user.Orders)
                .HasForeignKey(order => order.UserId);

            // Отзыв (ReviewModel) связан с пользователем (UserModel) и продуктом (ProductModel)
            modelBuilder.Entity<ReviewModel>()
                .HasOne(review => review.User)
                .WithMany(user => user.Reviews)
                .HasForeignKey(review => review.UserId);

            modelBuilder.Entity<ReviewModel>()
                .HasOne(review => review.Product)
                .WithMany(product => product.Reviews)
                .HasForeignKey(review => review.ProductId);

            // Роль (RoleModel) связана с пользователями (UserModel) и администраторами (AdminModel)
            modelBuilder.Entity<UserModel>()
                .HasOne(user => user.Role)
                .WithMany(role => role.Users)
                .HasForeignKey(user => user.RoleId);
        }

        //dotnet ef migrations add AddSeedData --startup-project ..\IS_Ever_Green

        //dotnet ef migrations add InitialCreate --startup-project ..\IS_Ever_Green
        //dotnet ef database update --startup-project ..\IS_Ever_Green
        //dotnet ef migrations remove --startup-project ..\IS_Ever_Green
    }
}
